package com.gskj.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gskj.commonutils.JwtUtils;
import com.gskj.commonutils.MD5;
import com.gskj.educenter.entity.MemberRegisterVO;
import com.gskj.educenter.entity.UcenterMember;
import com.gskj.educenter.mapper.UcenterMemberMapper;
import com.gskj.educenter.service.IUcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Connor-G
 * @since 2020-05-21
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements IUcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {

        // 手机号
        String mobile = member.getMobile();
        // 密码
        String password = member.getPassword();

        // 非空判断
        if (StringUtils.isEmpty(member) || StringUtils.isEmpty(password)) {
            throw new GuLiException(20001, "登陆失败,请正确输入有效值！");
        }

        // 判断手机号似乎否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        if (mobileMember == null) {
            throw new GuLiException(20001, "登陆失败,手机号错误！");
        }
        // 校验密码
        String memberPassword = mobileMember.getPassword();
        // 将密码加密在比较数据库中密码
        password = MD5.encrypt(password);

        if (!memberPassword.equals(password)) {
            throw new GuLiException(20001, "登陆失败,密码错误！");
        }

        // 判断用户是否禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuLiException(20001, "该会员暂时无法登陆,登陆失败！");
        }

        // 当以上条件都达成后，开始封装token令牌并返回token
        // 会员id
        String memberId = mobileMember.getId();
        // 会员 昵称
        String nickName = mobileMember.getNickname();
        // 生成 token
        String jwtToken = JwtUtils.getJwtToken(memberId, nickName);

        return jwtToken;
    }

    @Override
    public void registerMember(MemberRegisterVO member) {

        // 验证码
        String code = member.getCode();
        // 手机号
        String mobile = member.getMobile();
        // 昵称
        String nickName = member.getNickname();
        // 密码为加密
        String password = member.getPassword();

        if (StringUtils.isEmpty(code) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickName) ||
                StringUtils.isEmpty(password)) {
            throw new GuLiException(20001, "注册失败,请检查并输入有效值!");
        }

        // 根据 手机号 获取redis中存储的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);

        // 判断 redis中 验证码是否为值
        if (redisCode == null) {
            throw new GuLiException(20001, "注册失败,验证码5分钟已过期，请重新发送验证码!");
        }

        // 判断表单输入验证码是否与redis中一致
        if (!redisCode.equals(code)) {
            throw new GuLiException(20001, "注册失败,验证码输入错误,请重新填写!");
        }

        // 手机号唯一判断
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);

        int i = baseMapper.selectCount(wrapper);
        if (i > 0) {
            throw new GuLiException(20001, "注册失败,当前手机号已经注册过了!");
        }

        // 封装会员信息，并添加
        UcenterMember registerMember = new UcenterMember();
        // 密码加密
        password = MD5.encrypt(password);

        registerMember.setPassword(password);
        registerMember.setNickname(nickName);
        registerMember.setMobile(mobile);
        registerMember.setIsDisabled(false);// 用户不禁用，默认false
        // 默认头像
        registerMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");

        baseMapper.insert(registerMember);
    }

    @Override
    public UcenterMember getMemberByOpenId(String userOpenId) {

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", userOpenId);
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public int countRegisterDay(String day) {

        int i = baseMapper.countRegisterDay(day);
        return i;
    }
}
